package com.bawei.lib_common.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import me.jessyan.autosize.AutoSize

/**
 *author：yjk
 *description:
 *date：2025/6/9
*/
abstract class BaseFragment <VM:BaseViewModel,VDB: ViewDataBinding>: Fragment(){
    protected lateinit var mBinding: VDB //数据绑定
    protected lateinit var mParentView: View //view
    protected abstract val mLayoutResId:Int //布局id
    open val mPageCanBack:Boolean = true
    val mCanBack:Boolean //fragment嵌套
        get() {
            //可返回
            if(mPageCanBack){
                return true
            }
            //不可返回
            val fragments = childFragmentManager.fragments //当前fragment是否有子fragment
            if(fragments.isEmpty()){
                return false
            }
            for (fragment in fragments) {
                if(fragment is BaseFragment<*,*>){
                    if(fragment.isVisible){
                        return fragment.mPageCanBack
                    }
                }
            }
            return mPageCanBack
        }
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        AutoSize.autoConvertDensity(requireActivity(),360f,true)
        mBinding = DataBindingUtil.inflate(layoutInflater, mLayoutResId, container, false)
        mBinding.lifecycleOwner = viewLifecycleOwner
        mParentView = mBinding.root
        return mParentView
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
        initListener()
        initData()
    }
    open fun initView(){}
    open fun initData(){}
    open fun initListener(){}

    /**
     * 隐藏键盘
     */
    fun hideSoftKeyBroad(view:View){
        if(requireActivity() is BaseActivity<*,*>){
            (requireActivity() as BaseActivity<*,*>).hideKeyboard(view)
        }
    }
    /**
     * 关闭键盘
     */
    fun dismissKeyboard(){
        if(activity is BaseActivity<*,*>){
            (requireActivity() as BaseActivity<*,*>).dismissKeyboard()
        }
    }


    /**
     * 显示键盘
     */
    fun showSoftKeyBroad(view:View){
        if(activity is BaseActivity<*,*>){
            (requireActivity() as BaseActivity<*,*>).showSoftKeyBoard(view)
        }
    }

//    fun setLoadingViewVisible(show:Boolean,msg:String = ""){
//        if(activity is BaseActivity<*,*>){
//            (requireActivity() as BaseActivity<*,*>).setLoadingViewVisible(show,msg)
//        }
//    }
//    /**
//     * 通用状态监听：Loading显示和隐藏、错误提示
//     */
//    open fun addCommonStateListener(state: StateFlow<RequestEvent>){
//        state.observeWithLifecycle(this){
//            when(it){
//                is Loading -> {
//                    setLoadingViewVisible(true)
//                }
//                is Failed -> {
//                    setLoadingViewVisible(false)
//                    Toast.makeText(requireContext(),it.message, Toast.LENGTH_SHORT)
//                }
//                is Success -> {
//                    setLoadingViewVisible(false)
//                }
//                is Empty -> {
//                    setLoadingViewVisible(false)
//                    (requireActivity() as BaseActivity<*,*>).showToast("数据为空")
//                }
//            }
//        }
//    }

}